/**
 * Created by yangchen on 2019/4/4
 * */

/**
 * 大连 广州 贵州 陕西 北京 公共模块
 */
export default class DGGS {
  /**
   * hack年报填写数据 年报字段有变化
   * @param inputData 本年数据
   * @param lastYearData 上年数据
   * @param sheet 报表类型
   * @returns {{}}
   */
  static hackYearReportInput(inputData, lastYearData, sheet) {
    const result = {};
    for (let key in inputData) {
      if (Object.prototype.hasOwnProperty.call(inputData, key) && key.includes('bnljje')) {
        const realKey = key.replace('syxqylrbGrid.syxqylrbGridlb', 'syxqylrbnbGrid.xqylrbnbGridlb').replace('xqyxjllbGrid.xqyxjllbGridlb', 'xqyxjllbnGrid.xqyxjllbnGridlb');
        result[realKey] = inputData[key];
      }
    }
    for (let key in lastYearData) {
      if (Object.prototype.hasOwnProperty.call(lastYearData, key) && key.includes('bnljje')) {
        const realKey = key.replace('syxqylrbGrid.syxqylrbGridlb', 'syxqylrbnbGrid.xqylrbnbGridlb').replace('xqyxjllbGrid.xqyxjllbGridlb', 'xqyxjllbnGrid.xqyxjllbnGridlb');
        result[realKey.replace('bnljje', 'snje')] = lastYearData[key];
      }
    }
    return result;
  }

  /**
   * 填报界面只支持ie 切换chrome后 iframe绑定initFrame事件需要手动触发
   */
  static hackReportInputPage() {
    if (/\/cwbb|sbzs-cjpt-web\/biz/g.test(window.location.pathname)) {
      setTimeout(() => window.initFrame && window.initFrame(), 100);
    }
  }

  /**
   * 数据填报完成后 触发自动计算
   * @param inputData 实际填写数据
   * @param sheet 报表类型
   * @param isYearReport 是否为年报
   * @param iframeWindow 填写iframe的window对象
   */
  static tiggerDomByInputDataKey(inputData, sheet, isYearReport, iframeWindow) {
    // 触发自动计算的函数
    function trigger(DOM) {
      let event = document.createEvent('MouseEvents');
      event.initEvent('blur', true, true);
      DOM.dispatchEvent(event);
    }

    function tiggerDom(array) {
      for (let key in array) {
        let dom = iframeWindow.$(`[ng-model='${array[key]}']`)[0];
        if (dom) {
          trigger(dom);
        }
      }
    }

    let domArray = Object.keys(inputData);
    if (sheet === 'sds') {
      tiggerDom(domArray);
    } else if (sheet === 'balance') {
      tiggerDom(domArray.filter((item) => {
        return item.indexOf('qmye') !== -1;
      }));
      tiggerDom(domArray.filter((item) => {
        return item.indexOf('ncye') !== -1;
      }));
    } else {
      if (isYearReport) {
        tiggerDom(domArray.filter((item) => {
          return item.indexOf('snje') !== -1;
        }));
      } else {
        tiggerDom(domArray.filter((item) => {
          return item.indexOf('byje') !== -1;
        }));
      }
      tiggerDom(domArray.filter((item) => {
        return item.indexOf('bnljje') !== -1;
      }));
    }
  }

  /**
   * 判断是否为小微企业 汇算清缴
   */
  static isHsqjSmall() {
    const iframe = getHsqjIframe();
    if (iframe) {
      const title = iframe.document.querySelector('.NewTableHead').innerText || '';
      return !title.includes('一般企业');
    }
    return false;
  }
}

function getHsqjIframe() {
  return window.frames.frmMain.frames.frmSheet;
}
